<?php

namespace App\Model;


use Illuminate\Http\Request;

class IncreaseKkValueLogs extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'increase_kk_value_log';
    protected $fillable = ['user_id', 'uuid', 'nickname', 'kk_value', 'desc', 'kk_balance'];

    public function getList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $begin_date = $request->input('begin_date');
        $end_date = $request->input('end_date');
        $uuid = $request->input('uuid');

        $query = new self();
        if ($uuid) $query = $query->where('uuid', $uuid);
        if ($begin_date && $end_date) $query = $query->whereBetween('created_at', [$begin_date, $end_date]);
        if ($begin_date && !$end_date) $query = $query->where('created_at', '>=', $begin_date);
        if (!$begin_date && $end_date) $query = $query->where('created_at', '<=', $end_date);
        $datas = $query->orderBy('id', 'DESC')->paginate($limit, ['*'], 'page', $page);
        $datas = objToArray($datas);
        $datas['kk_total'] = $query->sum('kk_value');
        return $datas;
    }

    public function createData($request_arr, int $kk_balance)
    {
        return self::create(array_merge($request_arr, ['kk_balance' => $kk_balance]));
    }

}
